package LDD;


import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

public class LCC {
    static JLabel[][] juzhen = new JLabel[16][20]; //     һ  16x20 ı ǩ          ʾ    ˹      Ϸ    
    public static void main(String[] args) {
        
        JFrame frame = new JFrame("俄罗斯方块小游戏"); //           
        frame.setBounds(50,50,1200,1000); //    ô  ڴ С  λ  
        frame.setVisible(true); //    ô  ڿɼ 
        frame.setLayout(null); // ʹ þ  Բ   
        JPanel panel = new JPanel(new GridLayout(16,20)); //     һ  16  20 е        
        for(int i=0;i<16;i++) {
            for(int j=0;j<20;j++) {
                juzhen[i][j] = new JLabel(); //   ʼ  ÿ      Ϊһ    ǩ
                juzhen[i][j].setOpaque(true); //    ñ ǩ  ͸  
                Border border = BorderFactory.createLineBorder(Color.BLACK); //   ÿ    ǩ  Ӻ ɫ ߿ 
                juzhen[i][j].setBorder(border); //    ñ ǩ ߿ 
                panel.add(juzhen[i][j]); //     ǩ  ӵ      
            }
        }
        panel.setBounds(50,50,1000,800); //         λ úʹ С
        panel.setBackground(Color.BLUE); //       屳    ɫΪ  ɫ
        frame.add(panel); //        ӵ       
        JButton b1,b2,b3,b4; //      ĸ   ť
        
        b1 = new JButton("向左"); //          󡱰 ť
        b1.setBounds(50,870,125,40); //    ð ťλ úʹ С
        frame.add(b1); //     ť  ӵ       
        
        b2 = new JButton("向右"); //          ҡ   ť
        b2.setBounds(50+250,870,125,40); //    ð ťλ úʹ С
        frame.add(b2); //     ť  ӵ       
        
        b3 = new JButton("向下"); //          ¡   ť
        b3.setBounds(50+250*2,870,125,40); //    ð ťλ úʹ С
        frame.add(b3); //     ť  ӵ       
        
        b4 = new JButton("旋转"); //         ת    ť
        b4.setBounds(50+250*3,870,125,40); //    ð ťλ úʹ С
        frame.add(b4); //     ť  ӵ       

        Shape shape = new T(0,9); //     һ  L  ״ Ķ   ˹   飬  ʼλ  Ϊ(0,9)
        shape.show(juzhen); //  ڽ       ʾ    ״
		//        ӹ  ܴ   
		b1.addMouseListener(new MouseAdapter(){

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseClicked(e);
				shape.toLeft(juzhen);	
			}	
		});
		//        ӹ  ܴ   
		b2.addMouseListener(new MouseAdapter(){

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseClicked(e);
				shape.toRight(juzhen);	
			}
		});
		//        ӹ   
		b3.addMouseListener(new MouseAdapter(){

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseClicked(e);
				shape.toLow(juzhen);
			}		
		});
		//    ת      ӹ  ܴ   
		b4.addMouseListener(new MouseAdapter(){

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseClicked(e);
				shape.zhuan(juzhen);
			}		
		});
    }
}

class Cube{
    int x; //      x    
    int y; //      y    
    int color; //        ɫ  δʹ ã 
}

class Shape{
	//   һ    ת״̬    
	int status = 1;
    Cube[] arr = new Cube[4]; // һ    ״  4         
    public Shape(int x,int y) {
        //    캯      ʼ    ״  λ ã δʹ ã 
    }
    public void show(JLabel[][] juzhen) {
        for(int i=0;i<4;i++) {
            juzhen[arr[i].x][arr[i].y].setBackground(Color.red); //     ״  ÿ          Ϊ  ɫ
        }
    }
  //  ԭ    λ õ ͼ      
  	public void hide(JLabel[][] juzhen) {
  		for(int i=0;i<4;i++) {
  			juzhen[arr[i].x][arr[i].y].setBackground(Color.white);
  		}
  	}
	//  ͼ       ƶ 
	public void toLeft(JLabel[][] juzhen) {
		if(this.isInside())hide(juzhen);
		for (int i=0;i<4;i++) {
			arr[i].y-=1;
		}
		if(this.isOutOfLeft())this.toRight(juzhen);
		if(this.isInside())show(juzhen);
	}
	//  ͼ       ƶ 
	public void toRight(JLabel[][] juzhen) {
		if(this.isInside())hide(juzhen);
		for (int i=0;i<4;i++) {
			arr[i].y+=1;
		}
		if(this.isOutOfRight())this.toLeft(juzhen);
		if(this.isInside())show(juzhen);
	}
	//     ƶ 
	public void toLow(JLabel[][] juzhen) {
		if(this.isInside())hide(juzhen);
		for (int i=0;i<4;i++) {
			arr[i].x+=1;
		}
		if(this.isOutOfLow())this.toUp(juzhen);
		if(this.isInside())show(juzhen);
	}
	//图形向上
	public void toUp(JLabel[][] juzhen) {
		if(this.isInside())hide(juzhen);
		for (int i=0;i<4;i++) {
			arr[i].x-=1;
		}
		
		if(this.isInside())show(juzhen);
	}
	//  ͼ    ת
	public void zhuan(JLabel[][] juzhen) {
		
	}
	//判断图形是否超出左边界
	public boolean isOutOfLeft() {
		for (int i=0;i<4;i++) {
			if(arr[i].y<0)return true;
		}
		return false;
	}
	//判断图形是否超出右边界
	public boolean isOutOfRight() {
		for (int i=0;i<4;i++) {
			if(arr[i].y>19)return true;
		}
		return false;
	}
	//判断图形是否超出下边界
		public boolean isOutOfLow() {
			for (int i=0;i<4;i++) {
				if(arr[i].x>15)return true;
			}
			return false;
		}
		//判断图形是否在界内
		public boolean isInside() {
			return !this.isOutOfLeft()&&!this.isOutOfRight()&&!this.isOutOfLow();
		}
			
}

class T extends Shape{
    public T(int x,int y) {
        super (x,y); //    ø   Ĺ  캯  
        arr[0] = new Cube(); //   ʼ    һ       λ  
        arr[0].x = x;
        arr[0].y = y;
        arr[1] = new Cube(); //   ʼ   ڶ        λ  
        arr[1].x = x+1;
        arr[1].y = y;
        arr[2] = new Cube(); //   ʼ             λ  
        arr[2].x = x+1;
        arr[2].y = y+1;
        arr[3] = new Cube(); //   ʼ     ĸ      λ  
        arr[3].x = x+2;
        arr[3].y = y;
    }
    public void kongzhuan(JLabel[][]juzhen) {
    	if(status ==1) {
			arr[0].x +=1;
			arr[0].y +=1;
			
			arr[2].x +=1;
			arr[2].y -=1;
			
			arr[3].x -=1;
			arr[3].y -=1;
			status +=1;
		}else if(status ==2) {
			arr[0].x +=2;
			arr[0].y -=2;
			
			arr[2].x -=1;
			arr[2].y -=1;
			
			arr[3].x -=1;
			arr[3].y +=1;
			status +=1;
		}else if(status ==3) {
			arr[0].x -=2;
			arr[0].y -=2;
			
			arr[2].x -=1;
			arr[2].y +=1;
			
			arr[3].x +=1;
			arr[3].y +=1;
			status +=1;
		}else if(status ==4) {
			arr[0].x -=2;
			arr[0].y +=2;
			
			arr[2].x -=1;
			arr[2].y +=1;
			
			arr[3].x +=1;
			arr[3].y +=1;
			status =1;
		}
    }
public void zhuan(JLabel[][] juzhen) {
	if(super.isInside())hide(juzhen);
		if(status ==1) {
			arr[0].x +=1;
			arr[0].y +=1;
			
			arr[2].x +=1;
			arr[2].y -=1;
			
			arr[3].x -=1;
			arr[3].y -=1;
			status +=1;
		}else if(status ==2) {
			arr[0].x +=1;
			arr[0].y -=1;
			
			arr[2].x -=1;
			arr[2].y -=1;
			
			arr[3].x -=1;
			arr[3].y +=1;
			status +=1;
		}else if(status ==3) {
			arr[0].x -=1;
			arr[0].y -=1;
			
			arr[2].x -=1;
			arr[2].y +=1;
			
			arr[3].x +=1;
			arr[3].y +=1;
			status +=1;
		}else if(status ==4) {
			arr[0].x -=1;
			arr[0].y +=1;
			
			arr[2].x +=1;
			arr[2].y +=1;
			
			arr[3].x +=1;
			arr[3].y -=1;
			status =1;
		}
		if(!super.isInside()) {
			for(int i=0;i<3;i++)this.kongzhuan(juzhen);
		}
		if(super.isInside())show(juzhen);
	}
}